package com.mxt.moviedemo.service.impl;

import com.mxt.moviedemo.dao.DirectorsMapper;
import com.mxt.moviedemo.entity.Directors;
import com.mxt.moviedemo.entity.Geners;
import com.mxt.moviedemo.entity.PageRequest;
import com.mxt.moviedemo.entity.PageResult;
import com.mxt.moviedemo.service.DirectorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DirectorsServiceImpl implements DirectorsService {
    @Autowired
    private DirectorsMapper directorsMapper;

    @Override
    public PageResult<Directors> searchDirectorsByKeyword(PageRequest pageRequest, String keyword) {
        List<Directors> directors = directorsMapper.searchDirectorsByKeyword(pageRequest, keyword);
        int total = directorsMapper.getTotalDirectorsByKeyword(keyword);
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, directors);
    }

    @Override
    public Directors getDirectorById(int did) {
//        System.out.println(did);
        return directorsMapper.getDirectorById(did);
    }

    @Override
    public PageResult<Directors> getDirectorsByPage(PageRequest pageRequest) {
        List<Directors> directors = directorsMapper.getDirectorsByPage(pageRequest);
        // 过滤掉null元素
        List<Directors> nonNullDirectors = new ArrayList<>();
        for (Directors director : directors) {
            if (directors != null) {
                nonNullDirectors.add(director);
            }
        }
        int total = directorsMapper.getTotalDirectors();
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, nonNullDirectors);
    }




    @Override
    public List<Directors> searchDirectorsByName(String dname) {
        return directorsMapper.searchDirectorsByName(dname);
    }




    @Override
    public int addDirectors(Directors directors) {
        return directorsMapper.addDirectors(directors);
    }

    @Override
    public boolean updateDirectors(Directors directors) {
        return directorsMapper.updateDirectors(directors);
    }

    @Override
    public int deleteDirectors(int did) {
        return directorsMapper.deleteDirectors(did);
    }
}